import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Card, message, Tabs } from 'antd';
import EnergyPriceCard from '@/components/EnergyPriceCard';
import { setEnergyCategoryPrice } from '@/services/bajaService';
// import { ENERGYCATEGORY_PRICE } from '@/utils/constants';
import memoryUtils from '@/utils/memoryUtils';

const { TabPane } = Tabs;

class index extends Component {

  constructor(props) {
    super(props);
  }

  savePrice = async (prices, item)=> {
    const price = JSON.stringify(prices);
    await setEnergyCategoryPrice(item.navOrd, price);
    // ENERGYCATEGORY_PRICE[item.energyCategoryType] = price;
    memoryUtils.ENERGYCATEGORY_PRICE[item.$handle] = price;
    message.success('保存成功');
  }

  render() {
    const { energyCategories } = this.props;
    // console.log('render', energyCategories)
    return (
      <Tabs tabPosition={'left'} defaultActiveKey={0} onChange={this.handleChange}>
        {energyCategories.map((item, idx)=>(
          <TabPane tab={item.title} key={idx}>
            <Card title={item.title}>
              <EnergyPriceCard energyCategory={item} prices={JSON.parse(item.price)} savePriceCallback={(prices)=>this.savePrice(prices, item)}/>
            </Card>
          </TabPane>
        ))}
      </Tabs>
    );
  }
}

index.propTypes = {
  energyCategories: PropTypes.array.isRequired,
}

export default index;

